package cn.edu.lingnan.fruitshop.service.impl;

import cn.edu.lingnan.fruitshop.common.JSONResult;
import cn.edu.lingnan.fruitshop.mapper.GoodstableMapper;
import cn.edu.lingnan.fruitshop.pojo.Focustable;
import cn.edu.lingnan.fruitshop.mapper.FocustableMapper;
import cn.edu.lingnan.fruitshop.pojo.Goodstable;
import cn.edu.lingnan.fruitshop.pojo.User;
import cn.edu.lingnan.fruitshop.service.FocustableService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张小凡
 * @since 2022-01-04
 */
@Service
public class FocustableServiceImpl extends ServiceImpl<FocustableMapper, Focustable> implements FocustableService {

    @Autowired(required = false)
    private FocustableMapper focustableMapper;

    @Autowired(required = false)
    private GoodstableMapper goodstableMapper;

    //加入收藏
    @Override
    public JSONResult<Void> focus(Integer goodId, HttpSession session) {
        if(goodId==null){
            return new JSONResult<>(201,"该商品可能下架了");
        }
        User user =(User) session.getAttribute("user");
        if(user==null){
            return new JSONResult<>(202,"会话超时，需重新登录");
        }
        Integer uid=user.getUserId();
        //根据商品的id和uid查询focustable表
        QueryWrapper<Focustable> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("good_id",goodId).eq("user_id",uid);
        Focustable focustable = focustableMapper.selectOne(queryWrapper);
        if(focustable!=null){
            return new JSONResult<>(203,"该商品已被收藏");
        }
        //加入收藏  focustable表添加数据
        Focustable focustable1 = new Focustable().setGoodId(goodId).setUserId(uid).setFocusTime(new Date());
        int row = focustableMapper.insert(focustable1);
        if(row!=1){
            return new JSONResult<>(204,"收藏失败，稍后重试");
        }
        return new JSONResult<>(200,"收藏成功");
    }

    //查看我的收藏
    @Override
    public String myFocus(HttpSession session,Model model) {

        User user = (User) session.getAttribute("user");
        if(user==null){
            throw new RuntimeException("用户数据不存在");
        }
        Integer uid = user.getUserId();
//        Integer uid = 1;
        QueryWrapper<Focustable>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uid);
        List<Focustable> focustables = focustableMapper.selectList(queryWrapper);
        model.addAttribute("focustables",focustables);


        QueryWrapper<Goodstable>queryWrapper1 = new QueryWrapper<>();
        List<Goodstable> goodstableList = goodstableMapper.selectList(queryWrapper1);
        model.addAttribute("goodstableList",goodstableList);
        return "user/myFocus";
    }

    //删除收藏信息
    @Override
    public String deleteFocus(Integer focusId) {
        focustableMapper.deleteById(focusId);
        return "redirect:/focustable/myFocus";
    }
}
